/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mongoosehome.web.admin.action;

import com.mongoosehome.web.admin.service.ModuleService;
import com.mongoosehome.web.base.BaseActon;
import com.mongoosehome.web.common.domain.TModule;
import com.mongoosehome.web.common.domain.TRole;
import com.mongoosehome.web.common.domain.TUser;
import com.mongoosehome.web.enumcommon.PageSizeEnum;
import com.mongoosehome.web.login.service.UserService;
import com.mongoosehome.web.util.PageInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Administrator
 */
public class AdminAction extends BaseActon{

    private ModuleService moduleService;
    
    private UserService userService;
    
    private String treeStr;
    
    private PageInfo pageInfo;
    
    private Integer pageNum;    //页码
    
    private Integer numPerPage; //一页多少条数据
    
    private List<TRole> roleList = new ArrayList<TRole>();
    
    /**
     * 进入后台管理首页面
     * @return
     * @throws Exception 
     */
    @Override
    public String execute() throws Exception {
       
        List<Integer> ids = new ArrayList<Integer>();
        TUser user = (TUser)session.get("user");
        Set<TRole> roles = user.getRoles();
        if(roles != null && roles.size() > 0){
            for (TRole tRole : roles) {
                ids.add(tRole.getRoleId());
                roleList.add(tRole);
            }
        }
        //根据角色ids查找有权限的所有菜单信息
        Map<Integer,List<TModule>> mapModules = moduleService.getModuleByRoleIds(ids);
        //获取顶级菜单的列表信息
        List<TModule> modules = mapModules.get(null);
        StringBuilder tree = new StringBuilder();
        if(modules != null && modules.size() > 0){
            for (TModule tModule : modules) {
                tree = handlerRightTree(tModule,0,tree,mapModules,"normal");
            }
        }
//        System.out.println(tree.toString());
        setTreeStr(tree.toString());
        return SUCCESS;
    }
    
     public String moduleList() throws Exception {
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        //根据角色ids查找有权限的所有菜单信息
        Map<Integer,List<TModule>> mapModules = moduleService.getModuleByRoleIds(ids);
        //获取顶级菜单的列表信息
        List<TModule> modules = mapModules.get(null);
        StringBuilder tree = new StringBuilder();
        if(modules != null && modules.size() > 0){
            for (TModule tModule : modules) {
                tree = handlerRightTree(tModule,0,tree,mapModules,"normal");
            }
        }
        setTreeStr(tree.toString());
        return SUCCESS;
    }
    /**
     * 用户列表管理页面
     * @return
     * @throws Exception 
     */
    public String userList() throws Exception {
        if(getPageNum() == null){
            setPageNum(1);
        }
        if(getNumPerPage() == null){
            setNumPerPage(PageSizeEnum.PAGE_SIZE.getValue());
        }
        System.out.println(pageNum+":"+numPerPage);
        pageInfo = userService.getUserByPage(pageNum, numPerPage);
        return SUCCESS;
    }
    
    /**
     * 拼接左边菜单树的字符串
     * @param tModule
     * @param level
     * @param tree
     * @param map
     * @return 
     */
    private StringBuilder handlerRightTree(TModule tModule,int level,StringBuilder tree,Map<Integer,List<TModule>> map,String flag){
        //取得该节点下的子节点
        List<TModule> childrens = map.get(tModule.getModuleId());
         if(level == 0){
            tree.append("<div class=\"accordionHeader\">");
                tree.append("<h2><span>Folder</span>").append(tModule.getModuleName()).append("</h2>");
            tree.append("</div>");
            tree.append("<div class=\"accordionContent\">");
         }else if(level == 1){
            tree.append("<ul class=\"tree treeFolder\">");
            tree.append("<li>");
            if(tModule.getModuleUrl()!= null && !"".equals(tModule.getModuleUrl())){
                tree.append("<a href=\"");
                tree.append(tModule.getModuleUrl());
                int begin = tModule.getModuleUrl().lastIndexOf("/");
                int end = tModule.getModuleUrl().lastIndexOf(".");
                tree.append("\" target=\"navTab\" rel=\"").append(tModule.getModuleUrl().substring(begin+1, end)).append( "\">");
            }else{
                 tree.append("<a>");
            }
            tree.append(tModule.getModuleName());
            tree.append("</a>");
         }else{
            if("first".equals(flag)){
                tree.append("<ul>");
            }
            tree.append("<li>");
            if(tModule.getModuleUrl()!= null && !"".equals(tModule.getModuleUrl())){
                tree.append("<a href=\"");
                tree.append(tModule.getModuleUrl());
                int begin = tModule.getModuleUrl().lastIndexOf("/");
                int end = tModule.getModuleUrl().lastIndexOf(".");
                tree.append("\" target=\"navTab\" rel=\"").append(tModule.getModuleUrl().substring(begin+1, end)).append( "\">");
            }else{
                 tree.append("<a>");
            }
            tree.append(tModule.getModuleName());
            tree.append("</a>");
         }
        if(childrens != null && childrens.size() > 0){
            for(int i = 0;i< childrens.size();i++){
                if(i == 0){
                    tree = handlerRightTree(childrens.get(i),level+1,tree,map,"first");
                }else if(i == (childrens.size() - 1)){
                    tree = handlerRightTree(childrens.get(i),level+1,tree,map,"last");
                }else{
                    tree = handlerRightTree(childrens.get(i),level+1,tree,map,"normal");
                }
            }
        }
        if(level == 0){
            tree.append("</div>");
        }else if(level == 1){
            tree.append("</li>");
            tree.append("</ul>");
        }else{
            tree.append("</li>");
            if("last".equals(flag)){
                tree.append("</ul>");
            }
        }
         return tree;
    }
    
    public void setModuleService(ModuleService moduleService) {
        this.moduleService = moduleService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public String getTreeStr() {
        return treeStr;
    }

    public void setTreeStr(String treeStr) {
        this.treeStr = treeStr;
    }

    public PageInfo getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(PageInfo pageInfo) {
        this.pageInfo = pageInfo;
    }

    public Integer getNumPerPage() {
        return numPerPage;
    }

    public void setNumPerPage(Integer numPerPage) {
        this.numPerPage = numPerPage;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public List<TRole> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<TRole> roleList) {
        this.roleList = roleList;
    }
    
}
